﻿package 
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import org.leles.engines.beatgame.BeatGameEngine;
	import org.leles.engines.beatgame.BeatGameEvent;
	import org.leles.engines.beatgame.BeatGameNote;
	
	/**
	 * ...
	 * @author Bruno Leles
	 */
	public class Main extends Sprite
	{
		public var engine:BeatGameEngine;
		
		public var board:MovieClip;
		
		public function Main() 
		{
			super();
			engine = new BeatGameEngine();
			
			engine.addEventListener( BeatGameEvent.GAME_LOOP, _gameLoop )
			
			//engine.addEventListener( BeatGameEvent.SONG_STARTED, _notesOffset )
			//engine.addEventListener( BeatGameEvent.SONG_COMPLETED, _notesOffset )
			
			engine.addEventListener( BeatGameEvent.NOTE_ENTERED_OFFSET, _notesOffset )
			engine.addEventListener( BeatGameEvent.NOTE_LEAVED_OFFSET, _notesOffset )
			
			engine.start( new SampleSong () );
		}
		
		private function _gameLoop(e:BeatGameEvent):void 
		{
			board.tracks.x = - 100 * engine.currentTime;
		}
		
		private function _notesOffset(e:BeatGameEvent):void 
		{
			var beatNote:BeatGameNote = e.note;
			var movieNote:MovieClip;// = new Note;
					
			switch (e.type) 
			{
				case BeatGameEvent.NOTE_ENTERED_OFFSET:
					movieNote = new Note;
					
					movieNote.note = beatNote;
					beatNote.tag = movieNote;
					
					//
					movieNote.x = 350 + 100 * beatNote.time;
					movieNote.duration.width = 100 * beatNote.duration + .00000000000000001; //  just a lazy 0 fixer
					board.tracks['t' + beatNote.track].addChild( movieNote );
					break;
				case BeatGameEvent.NOTE_LEAVED_OFFSET:
					movieNote = beatNote.tag as MovieClip;
					
					board.tracks['t' + beatNote.track].removeChild( movieNote );
					break;
				
			}
		}
		
		
	}
	
}